//
//  NSDate+Helen.m


#import "NSDate+Helen.h"

@implementation NSDate (Helen)

/**
 *  是否为今天
 */
- (BOOL)isToday
{
    //日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    
    return
    (selfCmps.year == nowCmps.year) &&
    (selfCmps.month == nowCmps.month) &&
    (selfCmps.day == nowCmps.day);
}

/**
 *  是否为昨天
 */
- (BOOL)isYesterday
{
    NSDate *nowDate = [[NSDate date] dateYMD];
    NSDate *selfDate = [self dateYMD];
    //获得nowDate和selfDate之间的差距
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    return cmps.day ==1;
    return NO;
}



/**
 *  是否为今年
 */
+ (BOOL)isThisYear
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitYear;
    
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:[NSDate date]];
    
    return nowCmps.year == selfCmps.year;
}



//返回一个只有年月日的时间
-(NSDate *)dateYMD
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    NSString *selfStr = [fmt stringFromDate:self];
    return [fmt dateFromString:selfStr];
}



//返回一个只有年月日的时间字符串
+ (NSString *)stringYMD_FromDate:(NSDate *)date
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return destDateString;
}


//返回一个只有时分的时间字符串
+ (NSString *)stringHM_FromDate:(NSDate *)date
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"HH:mm"];
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return destDateString;
}



//返回一个只有时分秒的时间字符串
+ (NSString *)dateStrWithHMS
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"HH:mm:ss";
    NSString *selfStr = [fmt stringFromDate:[NSDate date]];
    return selfStr;
}



//获得与当前时间的差距
+ (NSDateComponents *)deltaWithNow
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calendar components:unit fromDate:[NSDate date] toDate:[NSDate date] options:0];
}

+ (NSString *)dateFormatterHMWithTimeStamp:(NSTimeInterval)timeStamp
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"HH:mm";
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    return [fmt stringFromDate:confromTimesp];
}


//把时间戳转化为时间(YYYY-MM-dd)
+ (NSString *)dateFormatterYMDWithTimeStamp:(NSTimeInterval)timeStamp
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"YYYY-MM-dd";
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    return [fmt stringFromDate:confromTimesp];
}


//把时间戳转化为时间(YYYY-MM-dd HH:mm)
+ (NSString *)dateFormatterYMDHMWithTimeStamp:(NSTimeInterval)timeStamp
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"YYYY-MM-dd HH:mm";
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    return [fmt stringFromDate:confromTimesp];
}

//把时间戳转化为时间(YYYY-MM-dd HH:mm:ss)
+ (NSString *)dateFormatterYMDHMSWithTimeStamp:(NSTimeInterval)timeStamp
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    return [fmt stringFromDate:confromTimesp];
}


/**
 *  输入的日期字符串形如：@"1992-05-21"
 */
+ (NSDate *)dateFromString_ymd:(NSString *)string_ymd
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    NSDate *destDate= [dateFormatter dateFromString:string_ymd];
    return destDate;
}



/**
 *  输入的日期字符串形如：@"1992-05-21 13:08:08"
 */
+ (NSDate *)dateFromString_ymdhms:(NSString *)string_ymdhms
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    NSDate *destDate= [dateFormatter dateFromString:string_ymdhms];
    return destDate;
}




@end
